package com.allinpay.merchantweb.common.utils;

import java.io.UnsupportedEncodingException;
/**
 * url转化 ASCII编码
 * @date 20180130
 * @author yuyj
 *
 */

public class CodeUtil {
	  
	/**
     * Byte数组转十六进制字符串字节间不用空格分隔
     * @param b
     * @return
     */
	public static String bytes2HexString(byte[] b) {
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}
	
	/**
	 * 将指定字符串src，以每两个字符分割转换为16进制形式 如："2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF,
	 * 0xD9}
	 * 
	 * @param src
	 *            String
	 * @return byte[]
	 */
	public static byte[] hexString2Bytes(String src) {
		if (src.length() % 2 != 0) {
			src = src + "0";
		}
		byte[] ret = new byte[src.length() / 2];
		byte[] tmp = src.getBytes();
		for (int i = 0; i < (src.length() / 2); i++) {
			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
		}
		return ret;
	}
	
	/**
	 * 将两个ASCII字符合成一个字节； 如："EF"--> 0xEF
	 * 
	 * @param src0
	 *            byte
	 * @param src1
	 *            byte
	 * @return byte
	 */
	public static byte uniteBytes(byte src0, byte src1) {
		byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
				.byteValue();
		_b0 = (byte) (_b0 << 4);
		byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
				.byteValue();
		byte ret = (byte) (_b0 ^ _b1);
		return ret;
	}
	
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		String url="http://mallinpayhk.morexge.com/goods/details/no/ISZHYUHEIJ00?from=groupmessage&isappinstalled=0";
		String  hexStr=bytes2HexString(url.getBytes());
		System.out.println("hexStr:"+hexStr);
		byte[] newStr =  hexString2Bytes(hexStr);
		String s;
		s = new String(newStr);
		System.out.println("newStr:"+s);
		
	}
	
	
	



}
